<template>
  <el-table :data="permissionArr" border row-key="id">
    <el-table-column prop="name" label="名称" align="center"></el-table-column>
    <el-table-column
      prop="code"
      label="权限值"
      align="center"
    ></el-table-column>
    <el-table-column
      prop="updateTime"
      label="修改时间"
      align="center"
    ></el-table-column>
    <el-table-column label="操作" align="center">
      <template #="{ row, $index }">
        <el-button
          :disabled="row.level == 4 ? true : false"
          type="primary"
          plain
          icon="Plus"
          size="small"
          @click="addPermission(row)"
        >
          {{ row.level == 3 ? '添加功能' : '添加菜单' }}
        </el-button>
        <el-button
          type="success"
          plain
          icon="Edit"
          size="small"
          :disabled="row.level == 1 ? true : false"
          @click="updatePermission(row)"
        >
          编辑
        </el-button>
        <el-popconfirm
          @confirm="deletePermission(row.id)"
          :title="`您确定要删除${row.name}吗?`"
          width="220px"
        >
          <template #reference>
            <el-button
              type="danger"
              plain
              icon="Delete"
              size="small"
              :disabled="row.level == 1 ? true : false"
            >
              删除
            </el-button>
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <el-dialog
    v-model="dialogFormValue"
    :title="
      menuData.id ? '更新菜单' : menuData.level == 4 ? '添加功能' : '添加菜单'
    "
  >
    <el-form label-width="100px">
      <el-form-item label="名称">
        <el-input
          v-model="menuData.name"
          placeholder="请您输入菜单名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="权限值">
        <el-input
          v-model="menuData.code"
          placeholder="请您输入权限值"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogFormValue = false">取消</el-button>
      <el-button type="primary" @click="save">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue'
import {
  reqRemoveMenu,
  reqAllPermission,
  reqAddOrUpdateMenu,
} from '@/api/acl/menu'
import type {
  PermissionResponseData,
  PermissionList,
  Permission,
  MenuParams,
} from '@/api/acl/menu/type'
import { ElMessage, rowContextKey } from 'element-plus'
let permissionArr = ref<PermissionList>([])
let dialogFormValue = ref<boolean>(false)
// 添加/修改携带的参数
let menuData = reactive<MenuParams>({
  code: '',
  level: 0,
  name: '',
  pid: 0,
  id: 0,
})
onMounted(() => {
  getPermissionList()
})
// 获取菜单数据方法
const getPermissionList = async () => {
  const result: PermissionResponseData = await reqAllPermission()
  if (result.code == 200) {
    permissionArr.value = result.data
  } else {
    ElMessage({
      type: 'error',
      message: '获取职位信息失败',
    })
  }
}
// 添加功能/菜单
const addPermission = (row: Permission) => {
  dialogFormValue.value = true
  Object.assign(menuData, {
    code: '',
    level: 0,
    name: '',
    pid: 0,
  })
  // 收集新增的菜单的level数值
  menuData.level = row.level + 1
  // 新增子菜单
  menuData.pid = row.id as number
}
const updatePermission = (row: Permission) => {
  dialogFormValue.value = true
  Object.assign(menuData, row)
}
const save = async () => {
  const result = await reqAddOrUpdateMenu(menuData)
  if (result.code == 200) {
    dialogFormValue.value = false
    ElMessage({
      type: 'success',
      message: menuData.id ? '更新成功' : '添加成功',
    })
    getPermissionList()
  }
}
// 删除某个权限
const deletePermission = async (id: number) => {
  const result = await reqRemoveMenu(id)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    getPermissionList()
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败',
    })
  }
}
</script>

<style scoped>
</style>